<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <div class="imgside"><img
            src="../assets/src=http___img.sccnn.com_bimg_338_38041.jpg&refer=http___img.sccnn.png"></div>
        <span>零大魔王的杂货铺</span>
      </div>
      <el-button type='info' @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px':'200px'" >
        <div class="toggle-button el-icon-caret-right" @click="toggleCollapse"></div>
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#ffd04b" :unique-opened='true' :collapse="isCollapse"
        :collapse-transition="false" :router="true" :default-active="active">
          <!-- 一级菜单 -->
          <!-- 这里拼接空字符串是因为数值加上字符串类型就可以转化成字符串 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单的模板区 -->
            <template slot="title">
              <i :class="iconsObj[item.id]" class="iconmg"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+subItem.path+''" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/'+subItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧区域主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 左侧菜单数据
      menuList: [],
      iconsObj: {
        125: 'el-icon-s-custom',
        103: 'el-icon-s-check',
        101: 'el-icon-shopping-cart-1',
        102: 'el-icon-present',
        145: 'el-icon-coin'
      },
      // 是否折叠
      isCollapse: false,
      // 被激活的链接地址
      active: ''
    }
  },
  created () {
    this.getMenuList()
    this.active = window.sessionStorage.getItem('active')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // console.log(res)
      this.menuList = res.data
    },
    // 点击函数切换侧边栏
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的高亮函数
    saveNavState(active) {
      window.sessionStorage.setItem('active', active)
      this.active = active
    }
  }
}
</script>

<style lang="less" scoped>
  .home-container {
    height: 100%;
  }

  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;

    >div {
      display: flex;
      align-items: center;

      span {
        margin-left: 15px;
      }
    }
  }

  .el-aside {
    background-color: #333744;
    .el-menu {
      border-right: none;
    }
  }

  .el-main {
    background-color: #eaedf1;
  }

  .imgside {
    height: 61px;
    width: 58px;
  }

  .imgside>img {
    height: 100%;
    width: 100%;
  }
  .iconmg {
    margin-right: 30px;
  }
  // .toggle-button {
  //   background-color: #4a5064;
  //   font-size: 10px;
  //   line-height: 24px;
  //   color: #fff;
  //   text-align: center;
  //   // 这是字母间距
  //   letter-spacing: 10px;
  //   cursor: pointer;
  // }
</style>
